<template>
	<view>
		<view class="pannel">
			<u-form :model="form" ref="uForm" label-width="160">
				<u-form-item label="原密码" prop="password"><u-input v-model="form.password" /></u-form-item>
				<u-form-item label="新密码" prop="newPassword"><u-input v-model="form.newPassword" /></u-form-item>
				<u-form-item label="确认密码" prop="reNewPassword"><u-input v-model="form.reNewPassword" /></u-form-item>
			</u-form>
		</view>
		<button @click="submit">修改密码</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					password: '',
					newPsssword: '',
					reNewPassword: ''
				},
				rules: {
					password: [
						{
							required: true,
							message: '密码不能为空',
							trigger: ['blur','change']
						}
					],
					newPassword: [
						{
							required: true,
							message: '密码不能为空',
							trigger: ['blur','change']
						}
					],
					reNewPassword: [
						{
							required: true,
							message: '密码不能为空',
							trigger: ['blur','change']
						},
						{
							validator: (rule, value, callback) => {
								if(value === this.form.newPassword){
									callback()
								}else{
									callback(new Error('两次密码输入不一致'))
								}
							},
							trigger: ['blur','change']
						}
					]
				}
			};
		},
		methods: {
			submit(){
				this.$refs.uForm.validate(valid => {
					if(valid){
						uni.request({
							url: this.$baseurl + '/prod-api/api/common/user/resetPwd',
							method: 'PUT',
							header: {Authorization: uni.getStorageSync('token')},
							data:{
								oldPassword: this.form.password,
								newPassword: this.form.newPsssword
							},
							success: (res) => {
								if(res.data.code === 200){
									uni.showToast({
										title:'修改成功'
									})
									uni.switchTab({
										url:'/pages/user/user'
									})
								}else{
									console.log(res.data)
									uni.showToast({
										title:res.data.msg
									})
								}
							}
						})
						// uni.showToast({
						// 	title: '修改成功'
						// })
					}else{
						uni.showToast({
							title: '修改失败'
						})
					}
				})
			}
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules)
		}
	}
</script>

<style lang="scss">
page{
	background-color: #F7F7F7;
	view{
		padding: 20rpx;
		.pannel{
			background-color: #fff;
		}
		button{
			width: 200rpx;
			background-color: #ff9900;
			color: white;
			margin-top: 40rpx;
		}
	}
}
</style>
